package com.logistics.modules.warehouseManagement.controller;

import com.logistics.common.utils.R;
import com.logistics.modules.warehouseManagement.from.InitRepository;
import com.logistics.modules.warehouseManagement.from.InitializationRules;
import com.logistics.modules.warehouseManagement.service.InitRepositoryService;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import java.util.*;

/**
 * 初始化存储控制器
 *
 * @author 水穷处云起时
 * @date 2022/05/09
 */
@RestController
@Slf4j
@RequestMapping("warehouseManagement/initRepository")
public class InitRepositoryController {


    @Autowired
    private InitRepositoryService initRepositoryService;

    /**
     * 得到列表
     *
     * @return {@link R}
     */
    @GetMapping("/list")
    @RequiresPermissions("warehouseManagement:initRepository:list")
    public R getList() {
        List<InitRepository> InitRepositorys = initRepositoryService.queryAll();
        return  R.ok().put("InitRepositorys",InitRepositorys);
    }


    /**
     * 初始化席位
     *
     * @return {@link R}
     */
    @PostMapping("/init")
    @RequiresPermissions("warehouseManagement:initRepository:init")
    public R initializeWarehouse(@RequestBody InitializationRules initializationRules) {
        // 根据初始化规则生成席位
        initRepositoryService.initializeWarehouse(initializationRules);
        return R.ok();
    }

    /**
     * 根据导入的excel实现仓库的初始化
     *
     * @param file 文件
     * @return {@link R}
     */
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    @RequiresPermissions("warehouseManagement:initRepository:init")
    public R importExcel(@RequestPart("excel") MultipartFile file,
                         @RequestParam("repositoryId") Integer id) throws Exception {
        // 根据传进来的参数实现初始化
        initRepositoryService.initializeWarehouseByExport(file, id);
        return R.ok("仓库初始化成功!");
    }

}
